package com.upcode.weighing.gateway;

import com.upcode.weighing.gateway.filter.CaptchaZuulFilter;
import org.orclight.java.util.captha.CaptchaClient;
import org.orclight.java.util.captha.strategy.SimpleCaptchaStrategy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringCloudApplication
@EnableZuulProxy
@EnableSwagger2
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
@ComponentScan({"org.orclight.java.util.captha", "com.upcode.weighing.gateway"})
public class GatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }
//    @Bean
//    public CorsFilter corsFilter() {
//        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
//        final CorsConfiguration config = new CorsConfiguration();
//        config.setAllowCredentials(true);
//        config.addAllowedOrigin("*");
//        config.addAllowedHeader("*");
//        config.addAllowedMethod("OPTIONS");
//        config.addAllowedMethod("HEAD");
//        config.addAllowedMethod("GET");
//        config.addAllowedMethod("PUT");
//        config.addAllowedMethod("POST");
//        config.addAllowedMethod("DELETE");
//        config.addAllowedMethod("PATCH");
//        source.registerCorsConfiguration("/**", config);
//        return new CorsFilter(source);
//    }

    @Bean
    public CorsFilter corsFilter() {
        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        final CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        config.addAllowedOrigin("*");
        config.addAllowedHeader("*");
        config.addAllowedMethod("OPTIONS");
        config.addAllowedMethod("HEAD");
        config.addAllowedMethod("GET");
        config.addAllowedMethod("PUT");
        config.addAllowedMethod("POST");
        config.addAllowedMethod("DELETE");
        config.addAllowedMethod("PATCH");
        source.registerCorsConfiguration("/admin/**", config);
        source.registerCorsConfiguration("/pay/**", config);
        source.registerCorsConfiguration("/transact/**", config);
        source.registerCorsConfiguration("/fileserver/**", config);
        source.registerCorsConfiguration("/card/**", config);
        source.registerCorsConfiguration("/statistic/**", config);
        source.registerCorsConfiguration("/ministry/**", config);
        source.registerCorsConfiguration("/etcapp/app/**", config);
        source.registerCorsConfiguration("/etcysf/unionpay/**", config);
        source.registerCorsConfiguration("/message/**", config);
        source.registerCorsConfiguration("/trans/**", config);
        source.registerCorsConfiguration("/auth/**", config);
        source.registerCorsConfiguration("/etcreport/miniprogram/**", config);
        source.registerCorsConfiguration("/etcapp/issue/**", config);
        final CorsConfiguration config2 = new CorsConfiguration();
        return new CorsFilter(source);
    }
    @Bean
    public CaptchaZuulFilter captchaZuulFilter() {
        return new CaptchaZuulFilter();
    }
    @Bean
    public CaptchaClient captchaClient() {
        return CaptchaClient.create()
                .captchaStrategy(new SimpleCaptchaStrategy())
                .build();
    }
}
